package com.css.fxfzypg.modules.baseData.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.css.fxfzypg.constants.YNEnum;
import com.css.fxfzypg.modules.baseData.queryParams.ReservoirQueryParams;
import com.css.fxfzypg.modules.baseData.repository.ReservoirRepository;
import com.css.fxfzypg.modules.baseData.entity.ReservoirEntity;
import com.css.fxfzypg.modules.baseData.service.ReservoirService;
import com.css.fxfzypg.util.FileUtil;
import com.css.fxfzypg.util.PlatformDateUtils;
import com.css.fxfzypg.util.PlatformPageUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author y1fan
 * @create 2022-08-15 15:16
 */
@Service
public class ReservoirServiceImpl implements ReservoirService {


    @Resource
    private ReservoirRepository reservoirRepository;
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ReservoirEntity saveReservoir(ReservoirEntity reservoirEntity) {
        reservoirEntity.setCreateTime(PlatformDateUtils.getCurrentTimestamp());
        reservoirEntity.setIsdelete(YNEnum.Y.toString());
        return reservoirRepository.saveReservoir(reservoirEntity);
    }

    @Override
    public ReservoirEntity findReservoirById(String dataReservoirId) {
        return reservoirRepository.findReservoirById(dataReservoirId);
    }

    @Override
    public JSONObject getAllReservoirByParams(ReservoirQueryParams queryParams) {
        return PlatformPageUtils.formatPageData(reservoirRepository.getAllReservoirByParams(queryParams));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteReservoir(List<Integer> ids) {

        reservoirRepository.deleteReservoir(ids);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void exportExcel(HttpServletResponse response) throws IOException {
        List<ReservoirEntity> list = reservoirRepository.exportExcel();
        FileUtil.exportExcel(list, "水库数据", "sheet", ReservoirEntity.class, "RepositoryExcel", response);

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void importExcel(MultipartFile file) throws IOException {
        List<ReservoirEntity> reservoirEntities = FileUtil.importExcel(file, ReservoirEntity.class);
        for (ReservoirEntity reservoirEntity : reservoirEntities) {
            reservoirEntity.setIsdelete(YNEnum.Y.toString());
            reservoirEntity.setCreateTime(PlatformDateUtils.getCurrentTimestamp());
        }
        reservoirRepository.importExcel(reservoirEntities);
    }
}
